#include "rfid_spi.h"

#if _RFID_FUNCTION_ENABLE_

uint8_t RfidSpiReadWriteByte(uint8_t Byte)
{
  uint8_t ReciveData = 0;

  for (uint8_t i = 0; i < 8; i++)
  {
    if (Byte & 0x80)
    {
      RfidSetMosiPinLevel(true);
    }
    else
    {
      RfidSetMosiPinLevel(false);
    }

    Byte <<= 1;
    RfidSetSckPinLevel(true);

    ReciveData <<= 1;
    if (RfidGetMisoPinLevel() == true)
    {
      ReciveData |= 0x01;
    }

    RfidSetSckPinLevel(false);
  }

  return ReciveData;
}

uint8_t RfidReadRawRC(uint8_t Address)
{
  uint8_t ReadResult = 0, AddrOffset = 0;

  AddrOffset = ((Address << 1) & 0x7E) | 0x80;

  RfidSetNssPinLevel(false);

  RfidSpiReadWriteByte(AddrOffset);
  ReadResult = RfidSpiReadWriteByte(0x00);

  RfidSetNssPinLevel(true);

  return ReadResult;
}

void RfidWriteRawRC(uint8_t Address, uint8_t Value)
{
  uint8_t AddrOffset = 0;

  AddrOffset = (Address << 1) & 0x7E;

  RfidSetNssPinLevel(false);

  RfidSpiReadWriteByte(AddrOffset);
  RfidSpiReadWriteByte(Value);

  RfidSetNssPinLevel(true);
}

#endif
